package com.pxene.hbase2hbase;

import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;

public class Hbase2HBaseMapper extends TableMapper<Text, Text> {

	private Text resultKey = new Text();
	private Text resultVal = new Text();

	@Override
	protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
		StringBuilder sb = new StringBuilder();
		// 通过cell获取表中的列值
		for (Cell cell : value.listCells()) {
			String qualifier=new String(CellUtil.cloneQualifier(cell));
			String cellValue=new String(CellUtil.cloneValue(cell));
//			String val = Bytes.toString(Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength()));
			// 写入HDFS
			resultKey.set(Bytes.toString(value.getRow()));
			resultVal.set(qualifier+":"+cellValue);
			context.write(resultKey, resultVal);
		}
	}

}